package com.nononsenseapps.feeder.ui.home

import android.view.ContextMenu
import android.view.MenuInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.nononsenseapps.feeder.ui.R
import com.nononsenseapps.feeder.db.room.ID_UNSET
import com.nononsenseapps.feeder.model.FeedUnreadCount
import com.nononsenseapps.feeder.util.DateUtils

class TagHolder(private val onNavigationItemClickListener: OnNavigationItemClickListener,
                private val adapter: FeedsAdapter,
                v: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(v), View.OnClickListener, View.OnCreateContextMenuListener {
    val title: TextView = v.findViewById(R.id.tag_name)
    val unreadCount: TextView = v.findViewById(R.id.tag_unreadcount)
    val expander: ImageView = v.findViewById(R.id.tag_expander)
    var wrap: FeedUnreadCount? = null

    init {
        // expander clicker
        expander.setOnClickListener {
            if (adapter.toggleExpansion(wrap!!)) {
                expander.setImageResource(R.drawable.tinted_expand_less)
            } else {
                expander.setImageResource(R.drawable.tinted_expand_more)
            }
        }
        v.setOnClickListener(this)
        v.setOnCreateContextMenuListener(this)
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    override fun onClick(v: View) {
        wrap?.let {
            onNavigationItemClickListener.onNavigationItemClick(
                    ID_UNSET, it.tag, null, it.tag
            )
        }
    }

    override fun onCreateContextMenu(menu: ContextMenu?, v: View?, menuInfo: ContextMenu.ContextMenuInfo?) {
            v?.context?.let { context ->
                if (menu != null) {
                    MenuInflater(context).inflate(R.menu.contextmenu_feed_tag, menu)

                    menu.findItem(R.id.action_delete).setOnMenuItemClickListener {
                        wrap?.let {onNavigationItemClickListener.deleteFeedsInTag(it.tag)}
                        true
                    }

                    menu.findItem(R.id.action_read_all).setOnMenuItemClickListener {
                        wrap?.let {onNavigationItemClickListener.readAllWithFeedTag(it.tag)}
                        true
                    }

                    menu.findItem(R.id.action_sync).setOnMenuItemClickListener {
                        wrap?.let {onNavigationItemClickListener.syncFeedWithTag(it.tag)}
                        true
                    }

                    menu.findItem(R.id.action_toggle_feed_read_able).setOnMenuItemClickListener {
                        wrap?.let {
                            onNavigationItemClickListener.toggleFeedWithTagReadable(it.tag,true)
                        }
                        true
                    }
                    menu.findItem(R.id.action_toggle_feed_unread_able).setOnMenuItemClickListener {
                        wrap?.let {
                            onNavigationItemClickListener.toggleFeedWithTagReadable(it.tag,false)
                        }
                        true
                    }

                    val tag= wrap!!.tag
                    menu.findItem(R.id.action_before_read_hour).setOnMenuItemClickListener {
                        val hourBefore = DateUtils.covertFormCurrentDate(DateUtils.COVENT_HOUR)
                        onNavigationItemClickListener.readAllWithFeedTag(tag,hourBefore)
                        true
                    }
                    menu.findItem(R.id.action_before_read_today).setOnMenuItemClickListener {
                        val hourBefore = DateUtils.covertFormCurrentDate(DateUtils.COVENT_DAY)
                        onNavigationItemClickListener.readAllWithFeedTag(tag,hourBefore)
                        true
                    }
                    menu.findItem(R.id.action_before_read_three_day).setOnMenuItemClickListener {
                        val hourBefore = DateUtils.covertFormCurrentDate(DateUtils.COVENT_THREE_DAY)
                        onNavigationItemClickListener.readAllWithFeedTag(tag,hourBefore)
                        true
                    }
                    menu.findItem(R.id.action_before_read_weak).setOnMenuItemClickListener {
                        val hourBefore = DateUtils.covertFormCurrentDate(DateUtils.COVENT_WEEK)
                        onNavigationItemClickListener.readAllWithFeedTag(tag,hourBefore)
                        true
                    }
                    menu.findItem(R.id.action_before_read_month).setOnMenuItemClickListener {
                        val hourBefore = DateUtils.covertFormCurrentDate(DateUtils.COVENT_MONTH)
                        onNavigationItemClickListener.readAllWithFeedTag(tag,hourBefore)
                        true
                    }
                    menu.findItem(R.id.action_before_read_custom).setOnMenuItemClickListener {
                        true
                    }
                }
            }
    }
}
